﻿using System.Collections.Generic;
using System.Linq;
using Diploma.Models.IRepositories;

namespace Diploma.Models.Repositories
{
    internal class QualificationLevelsRepository : IQualificationLevelsRepository
    {
        private readonly DiplomaEntities dataContext;

        public QualificationLevelsRepository(DiplomaEntities dataContext)
        {
            this.dataContext = dataContext;
        }

        public QualificationLevel GetLevel(int id)
        {
            return dataContext.QualificationLevels.FirstOrDefault(l => l.QualificationLevelId == id);
        }

        public List<QualificationLevel> GetAllLevels()
        {
            return dataContext.QualificationLevels.OrderBy(l => l.Name).ToList();
        }

        public Dictionary<QualificationLevel, List<Student>> GetAllLevelsWithStudents(int levelId)
        {
            IQueryable<Student> students = dataContext.Users.OfType<Student>().Where(s => s.DeletionDate == null);
            IQueryable<IGrouping<QualificationLevel, Student>> grouped = from s in students
                                                                         where s.StudentsGroup.QualificationLevelId == levelId
                                                                         group s by s.StudentsGroup.QualificationLevel
                                                                         into g 
                                                                         select g;
            Dictionary<QualificationLevel, List<Student>> v = grouped.ToDictionary(g => g.Key,
                                                                                   g =>
                                                                                   g.OrderBy(s => s.Surname).ThenBy(
                                                                                       s => s.Name).ThenBy(
                                                                                           s => s.Patronymic).ToList());
            return v;
        }

        public Dictionary<QualificationLevel, IEnumerable<StudentsGroup>> GetStudentGroupsGrouped(
            QualificationLevel qualificationLevel)
        {
            var collection = new Dictionary<QualificationLevel, IEnumerable<StudentsGroup>>
                                 {
                                     {
                                         qualificationLevel,
                                         qualificationLevel.StudentsGroups.Where(sg => sg.DeletionDate == null)
                                         }
                                 };

            return collection;
        }

        public IEnumerable<StudentsGroup> GetStudentGroups(QualificationLevel qualificationLevel)
        {
            return qualificationLevel.StudentsGroups.Where(sg => sg.DeletionDate == null);
        }

        public IEnumerable<GlobalTask> GetGlobalTasks(QualificationLevel level)
        {
            return level.GlobalTasks.Where(gt => gt.DeletionDate == null);
        }
    }
}